#include "../Demo/state_demo.h"

StateDemo::StateDemo(){
    Loger = new Log;
    Con = new Console;
    KeyIn = new Input;
    console_show = false;
    console_key = false;
}

StateDemo::~StateDemo(){

}

void StateDemo::Init(StateMenager *a_stmen,SDL_Surface *a_S_SCREEN){
    stmen = a_stmen;
    S_SCREEN = a_S_SCREEN;
    if(S_SCREEN == NULL){
        Loger->LogText("Fatal error: SDL init fail");
        stmen->UpdateAlive(false);
    }
    else {
        Loger->LogText("SDL init OK");
    }
    TTF_Init();
    Loger->LogText("TTF init OK");
    Re_blank = {0,0,1280,720};
    SDL_WM_SetCaption("Minute 0.1 Alfa",NULL);
    Font1 = TTF_OpenFont("Data/Font/SegoeL.ttf",12);
    if(Font1 == false){
        Loger->LogText("Error: Cant load Data/Font/SegoeL.ttf");
        MessageBox(NULL,"Error: Cant load Data/Font/SegoeL.ttf","Error",MB_ICONSTOP|MB_OK);
        stmen->UpdateAlive(false);
    }
    else {
        Loger->LogText("Load: Data/Font/SegoeL.ttf");
    }
    Font2 = TTF_OpenFont("Data/Font/System.fon",12);
    if(Font2 == false){
        Loger->LogText("Error: Cant load Data/Font/System.fon");
        MessageBox(NULL,"Error: Cant load Data/Font/System.fon","Error",MB_ICONSTOP|MB_OK);
        stmen->UpdateAlive(false);
    }
    else {
        Loger->LogText("Load: Data/Font/System.fon");
    }
    Color_Dtext = {255,255,255};
    Color_Dtext_bg = {0,0,0};
    Re_Dtext1 = {1,1,0,0};
    Re_Dtext2 = {1,15,0,0};
    S_Dtext1 = TTF_RenderText_Shaded(Font1,"Minute v0.1 (DELTA-RAY 1)",Color_Dtext,Color_Dtext_bg);
    S_Dtext2 = TTF_RenderText_Shaded(Font1,"FPS: ",Color_Dtext,Color_Dtext_bg);
    S_Cbg = IMG_Load("Data/console_bg.png");
    if(S_Cbg == false){
        Loger->LogText("Error: Cant load Data/console_bg.png");
        MessageBox(NULL,"Error: Cant load Data/console_bg.png","Error",MB_ICONSTOP|MB_OK);
        stmen->UpdateAlive(false);
    }
    else {
        Loger->LogText("Load: Data/console_bg.png");
    }
    SDL_WM_SetIcon(IMG_Load("Data/Icon.png"),NULL);
    Con->Initc(S_SCREEN,Font2,S_Cbg);
    Con->NewMsg("Basic Init - OK!");
    fps_timer = clock();
}

void StateDemo::Tick(){
    cur_time = clock();
    if(cur_time - fps_timer > 1000){
        fps_timer = cur_time;
        fps2 = fps;
        fps = 0;
    }
}

void StateDemo::TickFast(){
    //keyinput
    KeyIn->UpdateKey();
    //console tick
    if(console_show == true)Con->Tickc(KeyIn);
    //key tick
    if(KeyIn->IsKeyDown() == true){
        if(KeyIn->GetLastKey() == KEY_ESCAPE) stmen->UpdateAlive(false);
        if(KeyIn->GetLastKey() == KEY_BACKQUOTE && console_key == false){
            console_show = !console_show;
            console_key = true;
        }
    }
    if(KeyIn->IsKeyUp() == true){
        if(KeyIn->GetLastKey() == KEY_BACKQUOTE)console_key = false;
    }
    if(console_show == false){
        //otherkey while console is not active
    }
    //quit event
    if(KeyIn->IsQuit() == true) stmen->UpdateAlive(false);
}

void StateDemo::Draw(){
    fps++;
    SDL_FillRect(S_SCREEN,&Re_blank,0x0000FF);
    sprintf((char*)tmp_fps.c_str(),"FPS: %4.2f",fps2);
    S_Dtext2 = TTF_RenderText_Shaded(Font1,tmp_fps.c_str(),Color_Dtext,Color_Dtext_bg);
    SDL_BlitSurface(S_Dtext1,NULL,S_SCREEN,&Re_Dtext1);
    SDL_BlitSurface(S_Dtext2,NULL,S_SCREEN,&Re_Dtext2);
    if(console_show == true)Con->Drawc();
    SDL_FreeSurface(S_Dtext2);
    SDL_Flip(S_SCREEN);
}

void StateDemo::Destroy(){
    Loger->LogText("STATE-DEMO DESTROY");
    delete Loger;
    delete Con;
    delete KeyIn;
}
